package com.hqw.mindanews;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.VerifySMSCodeListener;
import cn.pedant.SweetAlert.SweetAlertDialog;

import com.hqw.mindanews.application.SysApplication;
import com.hqw.mindanews.bean.User;
import com.hqw.mindanews.fragment.RightFragment;
import com.hqw.mindanews.log.Log;
import com.hqw.mindanews.utils.Toastor;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class RegistActivity extends Activity implements OnClickListener {

	private Toastor mToastor;
	private Button mGetverify;
	private TextView mPhone;
	private TextView mPassWord;
	private Button mRegistUser;
	private TextView mSMScode;
	private MyCount mc;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_regist);
		initView();
		SysApplication.getInstance().addActivity(this);
	}

	private void initView() {
		mToastor = new Toastor(this);
		mGetverify = (Button) findViewById(R.id.get_verify);
		mGetverify.setOnClickListener(this);
		mPassWord = (TextView) findViewById(R.id.regist_password);
		mPhone = (TextView) findViewById(R.id.regist_account);
		mRegistUser = (Button) findViewById(R.id.regist_user);
		mRegistUser.setOnClickListener(this);
		mSMScode = (TextView) findViewById(R.id.sms_code);

	}

	@Override
	public void onClick(View v) {
		String phoneNumber = mPhone.getText().toString();
		String passWord = mPassWord.getText().toString();
		String smsCode = mSMScode.getText().toString();
		switch (v.getId()) {
		case R.id.get_verify:

			if (phoneNumber.equals("") || passWord.equals("")) {
				new SweetAlertDialog(this).setContentText("选项不能为空哦").show();
				return;
			}

			checkIsEexit();// 检测 是否存在该用户 即手机号
			break;
		case R.id.regist_user:

			if (smsCode.equals("")) {
				new SweetAlertDialog(this).setTitleText("啊哦")
						.setContentText("您没填验证码呢").show();
				return;
			} else if (phoneNumber.equals("") || passWord.equals("")) {
				new SweetAlertDialog(this).setContentText("选项不能为空哦").show();
				return;
			}
			registUser();
			break;
		}

	}

	private void checkIsEexit() {
		BmobQuery<User> bmobQuery = new BmobQuery<User>();
		String phoneNumber = mPhone.getText().toString();
		bmobQuery.addWhereEqualTo("mobilePhoneNumber", phoneNumber);
		bmobQuery.findObjects(this, new FindListener<User>() {

			@Override
			public void onSuccess(List<User> users) {
				if (users.size() == 0) {
					getVerifyCode();
					mGetverify.setClickable(false);
					mc = new MyCount(30000, 1000);
					mc.start();
				} else {
					new SweetAlertDialog(RegistActivity.this).setContentText(
							"用户已存在").show();
					return;
				}
			}

			@Override
			public void onError(int arg0, String arg1) {
				Log.d("xiao", arg1);
				new SweetAlertDialog(RegistActivity.this).setContentText(
						"联网失败，请再次点击，或检查网络").show();
			}
		});
	}

	private void registUser() {
		String phoneNumber = mPhone.getText().toString();
		String smsCode = mSMScode.getText().toString();
		String passWord = mPassWord.getText().toString();
		if (phoneNumber.equals("") || passWord.equals("")) {
			new SweetAlertDialog(this).setContentText("选项不能为空哦").show();
			return;
		}
		
		User.signOrLoginByMobilePhone(RegistActivity.this, phoneNumber,
				smsCode, new LogInListener<User>() {

					@Override
					public void done(User user, BmobException arg1) {
						
						if (user != null) {
							
							user.setMobilePhoneNumber(mPhone.getText().toString());
							user.setMobilePhoneNumberVerified(true);
							user.setAdministrator(0);
							user.setPassword(mPassWord.getText().toString());
							
							user.update(RegistActivity.this, new UpdateListener() {
								
								@Override
								public void onSuccess() {
									Intent it = new Intent(RegistActivity.this, MainActivity.class);
									startActivity(it);
									RegistActivity.this.finish();
								}
								
								@Override
								public void onFailure(int arg0, String arg1) {
									new SweetAlertDialog(RegistActivity.this).setContentText("网络错误，请重试").show();
									
								}
							});
						}else {
							Log.i("bmob",arg1);
						}

					}

				});

	}

	private void getVerifyCode() {
		String phoneNumber = mPhone.getText().toString();
		BmobSMS.requestSMSCode(this, phoneNumber, "register",
				new RequestSMSCodeListener() {

					@Override
					public void done(Integer smsId, BmobException ex) {
						if (ex == null) {// 验证码发送成功
							Log.i("bmob", "短信id：" + smsId);// 用于查询本次短信发送详情
							new SweetAlertDialog(RegistActivity.this)
									.setTitleText("棒棒哒")
									.setContentText("验证码请求成功").show();
						}

					}
				});

	}

	/* 定义一个倒计时的内部类 */
	class MyCount extends CountDownTimer {
		public MyCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			mGetverify.setText("获取验证码");
			mGetverify.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			mGetverify.setText("请等待30秒(" + millisUntilFinished / 1000 + ")...");
		}
	}

}
